package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.api.article.AuthorControllerApi;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/author")
public class AuthorController implements AuthorControllerApi {

    @Resource
    private AuthorService authorService;

    /**
     * @param userId 用户id
     * @return 返回查询结果
     * @Description 根据用户id查询作者信息
     */
    @GetMapping("/findByUserId/{userId}")
    @Override
    public ApAuthor findByUserId(@PathVariable("userId") Integer userId) {
        return authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));
    }

    /**
     * @param apAuthor 作者实体类对象
     * @return 添加结果
     * @Description 保存作者
     */
    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        authorService.save(apAuthor);
        return ResponseResult.okResult();
    }

    /**
     * @param id 作者id
     * @return 查询结果
     * @Description 根据Id查询作者
     */
    @GetMapping("/one/{id}")
    @Override
    public ApAuthor findById(@PathVariable("id") Integer id) {
        return authorService.getById(id);
    }
}